apiready = function() {
seajs.use(["../../api", "../../jquery", "../../app","../../service"], function($api, $, app,service) {
    var vm = {
        el: '#app',
        data: {
          goodsList:[],
          page:1,
          types:3,
          noneGoodsList:[]
        },
        mounted: function() {
          var that = this
            this.init()
            app.refreshHeaderInfo(function() {
                that.init();
            });
        },
        methods: {
            init: function() {
                var that = this;
                this.page=1
                this.types=3
                this.getHomeShopGoods()
            },
            // 触底
            scrolltobottom:function(){
              if(this.offon){
                this.offon = false
                this.page+=1
                this.getHomeShopGoods()
              }
            },
            // 去逛逛
            toIndex:function(){
                api.sendEvent({
                    name: 'toIndex'
                });
            },
            // 前往商品详情
            toGoods_detail:function(goods_id){
              api.openWin({
                  name: 'goods_detail',
                  url: '../buy_goods/goods.html',
                  pageParam: {
                      goodsId: goods_id,
                      isSeller:0
                  },
                  reload:true
              });
            },
            // 获取商品信息
            getHomeShopGoods:function(){
              var that = this
              app.ajax(
                  service['getHomeShopGoods'], {
                      data: {
                          values: {
                            type:this.types,
                            page:this.page
                          }
                      }
                  },
                  function(res) {
                      if (res.code == 0) {
                        if(that.page==1){
                            if(res.data.length==0&&that.types==3){
                              that.types = 5
                              that.getNoneGoodsList()
                              return;
                            }
                            that.goodsList = res.data
                        }else{
                          if(res.data.length==0){
                            api.toast({
                                msg: '没有更多数据...',
                                duration: 2000,
                                location: 'bottom'
                            });
                          }else{
                            that.goodsList = that.goodsList.concat(res.data)
                            that.offon = false
                          }
                        }
                      }
                  })
            },
            // 猜你喜欢
            getNoneGoodsList:function(){
              var that = this
              app.ajax(
                  service['getHomeShopGoods'], {
                      data: {
                          values: {
                            type:this.types,
                            page:this.page
                          }
                      }
                  },
                  function(res) {
                      if (res.code == 0) {
                        if(that.page==1){
                            that.noneGoodsList = res.data
                        }else{
                          if(res.data.length==0){
                            api.toast({
                                msg: '没有更多数据...',
                                duration: 2000,
                                location: 'bottom'
                            });
                          }else{
                            that.noneGoodsList = that.noneGoodsList.concat(res.data)
                            that.offon = false
                          }
                        }
                      }
                  })
            },
            //前往购物车
            toCart: function(){
              app.openWin('buyCart',{},{reload: true})
            },
            // 回到顶部
            toHead:function(){
              api.pageUp({
                  top: true
              })
            },
            // 前往店铺
            toShop:function(out_user_id){
              api.openWin({
                  name: 'single_shop',
                  url: './single_shop.html',
                  pageParam:{
                    out_user_id:out_user_id
                  }
              });
            },
            // 前往商品详情
            toDetail:function(){

              api.openWin({
                  name: 'goods_detail',
                  url: '../buy_goods/goods.html',
                  pageParam:{
                    goodsId:39
                  },
                  reload:true
              });
            }
        }
    }

    var newVue = new Vue(vm);
    var even = function() {
      api.addEventListener({
          name:'scrolltobottom',
          extra:{
              threshold:0
          }
      }, function(ret, err){
          newVue.scrolltobottom()
      });
      api.addEventListener({
          name: 'setcollect'
      }, function(ret, err){
        newVue.init()
      });
    }
    even();
});

}
